package com.shop.cereshop.admin.service.sos.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.admin.dao.sos.CereSosPhoneDAO;
import com.shop.cereshop.admin.param.sos.SosPhoneIdParam;
import com.shop.cereshop.admin.param.sos.SosPhoneQueryParam;
import com.shop.cereshop.admin.param.sos.SosPhoneSaveParam;
import com.shop.cereshop.admin.param.sos.SosPhoneUpdateParam;
import com.shop.cereshop.admin.service.sos.CereSosPhoneService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.sos.CereSosPhone;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 一键急救电话Service实现
 */
@Slf4j
@Service
public class CereSosPhoneServiceImpl implements CereSosPhoneService {

    @Autowired
    private CereSosPhoneDAO cereSosPhoneDAO;

    @Override
    public void save(SosPhoneSaveParam param, CerePlatformUser user) throws CoBusinessException {
        ensureCityCodeUnique(param.getCityCode(), null);
        CereSosPhone entity = new CereSosPhone();
        BeanUtils.copyProperties(param, entity);
        String now = TimeUtils.yyMMddHHmmss();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        cereSosPhoneDAO.insert(entity);
    }

    @Override
    public void update(SosPhoneUpdateParam param, CerePlatformUser user) throws CoBusinessException {
        CereSosPhone db = cereSosPhoneDAO.selectById(param.getId());
        if (db == null) {
            throw new CoBusinessException(CoReturnFormat.LOGIC_ERROR_DATABASE);
        }
        ensureCityCodeUnique(param.getCityCode(), param.getId());
        CereSosPhone entity = new CereSosPhone();
        BeanUtils.copyProperties(param, entity);
        entity.setUpdateTime(TimeUtils.yyMMddHHmmss());
        cereSosPhoneDAO.updateById(entity);
    }

    @Override
    public void delete(SosPhoneIdParam param, CerePlatformUser user) throws CoBusinessException {
        int affect = cereSosPhoneDAO.deleteById(param.getId());
        if (affect == 0) {
            throw new CoBusinessException(CoReturnFormat.LOGIC_ERROR_DATABASE);
        }
    }

    @Override
    public Page<CereSosPhone> page(SosPhoneQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<CereSosPhone> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(param.getCityCode())) {
            wrapper.like(CereSosPhone::getCityCode, param.getCityCode());
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            wrapper.like(CereSosPhone::getPhone, param.getPhone());
        }
        if (StringUtils.isNotBlank(param.getRemark())) {
            wrapper.like(CereSosPhone::getRemark, param.getRemark());
        }
        wrapper.orderByDesc(CereSosPhone::getUpdateTime);
        List<CereSosPhone> list = cereSosPhoneDAO.selectList(wrapper);
        PageInfo<CereSosPhone> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public CereSosPhone detail(Long id) throws CoBusinessException {
        CereSosPhone entity = cereSosPhoneDAO.selectById(id);
        if (entity == null) {
            throw new CoBusinessException(CoReturnFormat.LOGIC_ERROR_DATABASE);
        }
        return entity;
    }

    /**
     * 校验地区编码唯一
     */
    private void ensureCityCodeUnique(String cityCode, Long excludeId) throws CoBusinessException {
        if (StringUtils.isBlank(cityCode)) {
            return;
        }
        LambdaQueryWrapper<CereSosPhone> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CereSosPhone::getCityCode, cityCode);
        if (excludeId != null) {
            wrapper.ne(CereSosPhone::getId, excludeId);
        }
        Integer count = cereSosPhoneDAO.selectCount(wrapper);
        if (count != null && count > 0) {
            throw new CoBusinessException(CoReturnFormat.UNIQUE_ERROR);
        }
    }
}
